package cn.crawler.app.web.modules.controllers.platform.crown;


import cn.crawler.app.crown.modules.models.CrownMatch;
import cn.crawler.app.crown.modules.services.CrownMatchService;
import cn.crawler.app.web.commons.ulog.annotation.SLog;
import cn.crawler.framework.page.Pagination;
import cn.crawler.framework.util.Result;
import cn.crawler.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.util.Date;

/**
 * 皇冠赛事Controller
 *
 * @author
 */
@IocBean
@At("/platform/crown/match")
public class CrownMatchController {

    @Reference
    @Inject
    private CrownMatchService matchService;


    @At("/list")
    @Ok("beetl:/platform/crown/match/list.html")
    @RequiresPermissions("crown.match")
    public void index() {
    }

    @At("/edit")
    @Ok("json")
    @RequiresPermissions("crown.match")
    public Object edit(@ParamVerify(required = true, name = "id") String id) {
        return Result.success(matchService.fetch(id));
    }

    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("crown.match.edit")
    @SLog(tag = "修改赛果")
    public Object editDo(@Param("..") CrownMatch match) {
        matchService.update(match);
        return Result.success();
    }

    /**
     * 赛事列表
     *
     * @param start
     * @param end
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @At("/data")
    @Ok("json:full")
    @RequiresPermissions("crown.match.list")
    public Object data(
            @Param("searchKeyword") String searchKeyword,
            @Param("stage") String stage,
            @Param("start") Date start,
            @Param("end") Date end,
            @Param("pageNumber") int pageNumber,
            @Param("pageSize") int pageSize) {
        Cnd cnd = Cnd.NEW();
        if (start != null) {
            cnd.and("dateTime", ">=", start);
        }
        if (end != null) {
            cnd.and("dateTime", "<=", end);
        }
        if (Strings.isNotBlank(stage)) {
            cnd.and("stage", "=", stage);
        }
        if (Strings.isNotBlank(searchKeyword)) {
            SqlExpressionGroup e1 = Cnd.exps("home", "like", "%" + searchKeyword + "%");
            SqlExpressionGroup e2 = Cnd.exps("custom", "like", "%" + searchKeyword + "%");
            SqlExpressionGroup e3 = Cnd.exps("league", "like", "%" + searchKeyword + "%");
            SqlExpressionGroup e4 = Cnd.exps("mid", "=", searchKeyword);
            cnd.and(e1.or(e2).or(e3).or(e4));
        }
        Pagination pagination = matchService.listPage(pageNumber, pageSize, cnd.orderBy("dateTime", "desc"));
        return Result.success(pagination);
    }

    /**
     * 手动爬取赛事赛果
     *
     * @param searchDate
     * @return
     */
    @At("/crawlMatchResult")
    @Ok("json")
    @RequiresPermissions("crown.match.edit")
    @SLog(tag = "爬取赛事赛果")
    public Object crawlMatchResult(@Param("searchDate") String[] searchDate) {
        if (null == searchDate || searchDate.length == 0) {
//            return Result.error("请选择要爬取的日期");
            return Result.error();
        }
        // 转毫秒
        Long startTime = Times.d2TS(Times.D(searchDate[0])) * 1000;
        Long endTime = Times.d2TS(Times.D(searchDate[1])) * 1000;
        matchService.crawlMatchResult(startTime, endTime);
        return Result.success();
    }

}
